package com.moli.iov.admin.web.controller;

import com.moli.iov.admin.constants.Constants;
import com.moli.iov.admin.model.vo.req.VehicleRegisterRecordVo;
import com.moli.iov.admin.service.IVehicleRegisterRecordService;
import com.moli.iov.base.model.dto.req.VehicleRegisterRecordDto;
import com.moli.iov.base.model.mongo.VehicleRegisterRecord;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.CommonUtils;
import com.moli.iov.util.ExcelUtil;
import com.moli.iov.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 终端日志
 *
 * @author wanghualin
 * @date 2019-12-13 09:55:34
 */
@Slf4j
@RequestMapping("/log")
@RestController
public class VehicleRegisterRecordController extends BaseController {
    @Autowired
    private IVehicleRegisterRecordService vehicleRegisterRecordService;

    /**
     * 日志查询> T-Box安装日志-list
     */
    @PostMapping("/vehicleRegisterRecord/list")
    public RestResponse listVehicleRegisterRecord(@RequestBody VehicleRegisterRecordVo vo) {
        log.info("日志查询> T-Box安装日志-列表 入参：{}", JsonUtils.toJson(vo));
        assembleUserInfo(vo);
        VehicleRegisterRecordDto dto = new VehicleRegisterRecordDto();
        CommonUtils.copyProperties(vo, dto);
        return vehicleRegisterRecordService
                .listVehicleRegisterRecord(dto);
    }
    /**
     * 日志查询> T-Box安装日志-导出
     */
    @PostMapping("/vehicleRegisterRecord/export")
    public RestResponse exportVehicleRegisterRecord(@RequestBody VehicleRegisterRecordVo vo, HttpServletResponse response) {
        log.info("日志查询> T-Box安装日志-导出 入参：{}", JsonUtils.toJson(vo));
        assembleUserInfo(vo);
        VehicleRegisterRecordDto dto = new VehicleRegisterRecordDto();
        CommonUtils.copyProperties(vo, dto);
        dto.setPageNum((long) 1);
        dto.setPageSize(Constants.EXPORT_PAGE_SIZE);
        RestResponse<PageExtend<VehicleRegisterRecord>> res = vehicleRegisterRecordService
                .listVehicleRegisterRecord(dto);
        if (res==null||!res.isFlag()) {
            return RestResponse.failure(res.getMessage());
        }
        if (res.getData() == null || res.getData().getRecords().isEmpty()) {
            return RestResponse.failure("数据为空");
        }
        String[] propNames = {"serialNumber", "vin", "license", "terminalModelName",
                "deviceId", "iccid", "registerTime"};
        String[] headStrs = {"操作流水号", "车架号", "车牌号", "T-Box型号", "T-BoxID", "ICCID", "注册时间"};
        String title = " T-Box安装日志";
        List data = res.getData().getRecords();
        try {
            ExcelUtil.exportDataExcel(request, response, data, propNames, headStrs, title);
            return RestResponse.success();
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.failure("导出异常");
        }
    }

}
